package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;
    @Autowired
    private MinioTemplate minioTemplate;
    @PostMapping("/list")
    public ResponseResult findByPage(PageRequest pageRequest, @RequestBody CourseDto dto){
        //调用Service
        return courseBaseService.findByPage(pageRequest,dto);
    }
    @PostMapping("/upload")
    public ResponseResult uploadImage(MultipartFile uploadFile){
        System.out.println(">>>>>>>>>>>>>>>>>"+uploadFile.getName());
        String url = minioTemplate.upload(uploadFile);
        return ResponseResult.okResult(url);
    }
    @PostMapping
    public ResponseResult addCourse(@RequestBody CourseBaseDto courseBaseDto) {
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        System.out.println(">>>>>>>>>>>>>"+authInfo.getCompanyId());


        return courseBaseService.addCourse(courseBaseDto);
    }
    @GetMapping("{courseBaseId}")
    public ResponseResult viewCourse(@PathVariable("courseBaseId")Long courseBaseId) {
        //1.调用service查询
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CourseBaseVo vo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);

        //3.返回结果
        return ResponseResult.okResult(vo);
    }
    @PutMapping
    public ResponseResult updateCourse(@RequestBody CourseBaseDto courseBaseDto){
        //1.调用service修改
        return courseBaseService.updateCourse(courseBaseDto);
    }
    @DeleteMapping("{courseBaseId}")
    public ResponseResult deleteCourse(@PathVariable("courseBaseId")Long courseBaseId) {

        //调用service删除
        return courseBaseService.deleteCourse(courseBaseId);
    }
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commitCourse(@PathVariable("courseBaseId")Long courseBaseId) {

        //调用service删除
        return courseBaseService.commitCourse(courseBaseId);
    }
    @PostMapping("/approve")
    public ResponseResult approveCourseCommit(@RequestBody CourseAuditDto courseAuditDto) {

        //调用service删除
        return courseBaseService.approveCourseCommit(courseAuditDto);
    }
    //课程下架
    @GetMapping("/offline/{courseId}")
    public ResponseResult offlineCourse(@PathVariable("courseId")Long courseId){

        //调用service删除
        return courseBaseService.offlineCourse(courseId);
    }
}
